﻿/**
 * Proyecto: Cognitum
 * Global IT Services 2008
 * @author
 */
Cognitum.Polizas.Siniestro.Entity = function(){
    if (LOG.isDebugEnabled()) {
        LOG.debug('[Cognitum.Polizas.Siniestro.Entity.ctor] <-');
    }
    Cognitum.Polizas.Siniestro.Entity.superclass.constructor.call(this);
}

Ext.extend(Cognitum.Polizas.Siniestro.Entity, Karma.Core.Entity, {
    id: 'Siniestro.Entity',
    name: 'Siniestros',
    editorXType: 'sin.editor',
    editorW: 670,
    editorH: 500,
    columns: [{
        Name: 'Id',
        Property: 'Id',
        Mostrar: false
    }, {
        Name: 'Numero',
        Property: 'Numero'
    }, {
        Name: 'Poliza Ot',
        Property: 'Poliza.Id'
    }, {
        Name: 'Poliza Folio',
        Property: 'Poliza.Folio'
    }, {
        Name: 'Ramo',
        Property: 'Poliza.Ramo.Nombre'
    }, {
        Name: 'SubRamo',
        Property: 'Poliza.SubRamo.Nombre'
}],
        titleProperty: new Ext.XTemplate('Numero {Numero}'),
        searchlist: {
            grouping: true,
            sortings: ['Id'],
            views: [{
                Id: 0,
                Nombre: 'Normal',
                Template: null
            }, {
                Id: 1,
                Nombre: 'Expandida',
                Template: null
}],
                previewTemplate: null,

                getTBarCustomControls: function() {
                    return ['->', {
                        text: 'Nuevo',
                        iconCls: 'icon-plus',
                        scope: this,
                        menu: {
                            items: [{
                                text: 'Autos',
                                handler: function() {
                                    this.fireEvent('newSiniestro', 'AUTO');
                                },
                                scope: this
                            }, {
                                text: 'GMM',
                                handler: function() {
                                    this.fireEvent('newSiniestro', 'GM');
                                },
                                scope: this
                            }, {
                                text: 'Vida',
                                handler: function() {
                                    this.fireEvent('newSiniestro', 'VID');
                                },
                                scope: this
                            }, {
                                text: 'Daños',
                                handler: function() {
                                    this.fireEvent('newSiniestro', 'DANHOS');
                                },
                                scope: this
}]
                            },
                            hidden: !this.security.New
                        }, '|', ' '];
                    }
                },
                aggregatelist: {
                    newRecord: function() {
                        if (PLOG.isDebugEnabled()) {
                            PLOG.debug('[Siniestro.AggregateList.newRecord] <-');
                        }
                        var ramo = this.parentEntity.Ramo.Clave;
                        var entidad = Karma.Core.ModuleManager.Instance.getEntity('Siniestros' + ramo);
                        entidad.NewFromEntity({
                            Id: this.parentEntity.Id
                        }, {
                            flush: {
                                fn: function(result, form) {
                                    this.reload();
                                },
                                scope: this
                            }
                        });
                    },
                    openRecord: function(id) {
                        if (PLOG.isDebugEnabled()) {
                            PLOG.debug('[Siniestro.AggregateList.openRecord] <-');
                            PLOG.debug('[Siniestro.AggregateList.openRecord] -- Ramo: ' + this.parentEntity.Ramo.Clave);
                        }
                        var ramo = this.parentEntity.Ramo.Clave;
                        var entidad = Karma.Core.ModuleManager.Instance.getEntity('Siniestros' + ramo);
                        entidad.Open(id, {
                            flush: {
                                fn: function(result, form) {
                                    this.reload();
                                },
                                scope: this
                            }
                        });
                    }
                },
                link: {
                    displayProperty: 'Numero',
                    tpl: '{Numero}'
                },
                NewSiniestroMethod: 'NewWithOutParams',
                getCustomListeners: function() {
                    return {
                        'newSiniestro': {
                            fn: function(Division) {
                                if (PLOG.isDebugEnabled()) {
                                    PLOG.debug('[Siniestro.Entity.newSiniestro]  <- ' + Division);
                                }
                                var entidad = null;
                                entidad = Karma.Core.ModuleManager.Instance.getEntity('Siniestros' + Division);
                                entidad.New();
                            },
                            scope: this
                        }
                    };
                },

                onOpen: function(id, listeners) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug('[Siniestro.Entity.onOpen2] <-');
                    }
                    this.useInvoker(Karma.Conf.GetMethod, id, {
                        fn: function(siniestro) {                        
                            if (LOG.isDebugEnabled()) {
                                LOG.debug('[Siniestro.Entity.onOpen2]-- poliza.DivisionOperativa=' + siniestro.Poliza.Ramo.DivisionOperativa);
                            }
                            var division = siniestro.Poliza.Ramo.DivisionOperativa;
                            var entidad = null;
                            if(division == 0)
                                entidad = Karma.Core.ModuleManager.Instance.getEntity('SiniestrosAUTO');
                            else if(division == 1)
                                entidad = Karma.Core.ModuleManager.Instance.getEntity('SiniestrosGM');
                            else if (division == 2)
                                entidad = Karma.Core.ModuleManager.Instance.getEntity('SiniestrosVID');
                            else if (division == 3)
                                entidad = Karma.Core.ModuleManager.Instance.getEntity('SiniestrosDANHOS');
                            entidad.openWindow(siniestro, null, {
                                isNew: false
                            });
                        },
                        scope: this
                    });
                }
            });

Cognitum.Siniestros = Cognitum.Polizas.Siniestro.Entity;
